const { MOCK } = require('../utils').settings
const axios = require('../utils/axios')
const { error } = require('../utils/error')
const {
  mockRank,
  mockRecommend,
  mockComment,
  mockNews,
  mockAddNews,
  mockComplain,
  mockReNews,
  mockEditReNews,
  mockAddComment
} = require('./mock/news')
const apiError = (msg) => {
  error('50000', __dirname, msg)
}
// news - rank
exports.requestRank = async () => {
  let result = MOCK ? mockRank : await axios.get('/getList')
  // let result = await mockRank
  if (!result) {
    apiError('requestRank')
    result = mockRank
  }
  return result
}
// news - recommend
exports.requestRecommend = async (data = {}) => {
  let result = MOCK
    ? mockRecommend
    : await axios.get('/getRecommendList', {
        params: data
      })
  // let result = await mockRecommend
  if (!result) {
    apiError('requestRecommend')
    result = mockRecommend
  }
  return result
}
// news - comment
exports.requestComment = async (data = {}) => {
  let result = MOCK
    ? mockComment
    : await axios.get('/getCommentsList', {
        params: data
      })
  // let result = await mockComment
  if (!result) {
    apiError('requestComment')
    result = mockComment
  }
  return result
}
// news - addComment
exports.requestAddComment = async (data = {}) => {
  let result = MOCK ? mockAddComment : await axios.post('/user/comment', data)
  // let result = await mockAddComment
  if (!result) {
    apiError('requestAddComment')
    result = mockAddComment
  }
  return result
}
// news - news
exports.requestNews = async (data = {}) => {
  let result = MOCK
    ? mockNews
    : await axios.get('/getArticle', {
        params: data
      })
  // let result = await mockNews
  if (!result) {
    apiError('requestNews')
    result = mockNews
  }
  return result
}
// news - addNews
exports.requestAddNews = async (data = {}) => {
  let result = MOCK ? mockAddNews : await axios.post('/user/contribute', data)
  // let result = await mockAddNews
  if (!result) {
    apiError('requestAddNews')
    result = mockAddNews
  }
  return result
}
// news - reNews *
exports.requestReNews = async (data = {}) => {
  // let result = MOCK ? mockReNews : await axios.get('/user/getEditContributeData', {
  //   params: data
  // })
  let result = await mockReNews
  if (!result) {
    apiError('requestReNews')
    result = mockReNews
  }
  return result
}
// news - editReNews *
exports.requestEditReNews = async (data = {}) => {
  // let result = MOCK ? mockEditReNews : await axios.post('', data)
  let result = await mockEditReNews
  if (!result) {
    apiError('requestEditReNews')
    result = mockEditReNews
  }
  return result
}

// news - complain
exports.requestComplain = async (data = {}) => {
  let result = MOCK ? mockComplain : await axios.post('/user/feedback', data)
  // let result = await mockComplain
  if (!result) {
    apiError('requestComplain')
    result = mockComplain
  }
  return result
}
